<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/icon.css"/>">

<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>

<title>员工管理</title>
</head>
<body>
<style>
		#hwc-staffitem{
			margin-top:10px;
		
		}
		#hwc-age-div{
			position:relative;
			left:156px;
			top:-20px;
			width:150px;
			height:15px;
		}
		
		
</style>
<script>
	$(function(){
		
		//打开添加按钮
		$("#hwc-addStaff").linkbutton({onClick:function(){
			
			$("#hwc-addstaffwin").window({'title':"添加员工"});
			$("#form-addStaff").form("clear");
			$("#hwc-addstaffwin").window("open");
			
		}});
		
		
		//自动填写性别年龄
		$("#hwc-idcard").textbox({
			onChange:function(){
				//取当前年
				var mydate = new Date();
				var year = new Date().getFullYear();
				//取IDcard的出生年份
				var idcardyear = $(this).textbox("getValue").substring(6,10);
				var age = year - idcardyear;
				$("#hwc-age").textbox("setValue",age); 
				//获取性别
				var idcardsex = $(this).textbox("getValue").substring(16,17);
				var pd = idcardsex % 2;
				if(pd != 0){
					$("#hwc-staffsex").combobox("select","男");
					
				}else{
					$("#hwc-staffsex").combobox("select","女");
				}
			}
		});
		
		
		//获取职称
			$("#hwc-staff-post").combogrid({
				onChange:function(newValue,oladValue){
					var postno = $("#hwc-staff-post").combogrid("getValue");
					
					 $("#hwc-staff-title").combogrid({
						url:'queryAlltitlePostno',
						queryParams:{
							postno:postno
						}
					}); 
			}});
			//提交添加或者编辑员工的请求
			$("#hwc-staff-okbtn").linkbutton({
				onClick : function(){
					var title = $('#hwc-addstaffwin').window("options").title;
					
					var url = (title == '添加员工' ? 'hwc_addNewStaff' : 'editStaff');
					
					$("#form-addStaff").form('submit',{
						url : url,
						onSubmit : function(){
							//验证
							return $(this).form("validate");
						},
					success : function(data){
						
						var json = eval("("+data+")");
						$.messager.alert("提示",json.reason);
						$("#hwc-addstaffwin").window('close');
						$("#hwc-staff").datagrid("reload");
						$(this).form("clear");
						
						}
					});
				}
			});
			
		//打开编辑员工的窗口
		$("#hwc-editStaff").linkbutton({onClick:function(){
				
				var rows = $("#hwc-staff").datagrid("getSelections");
				if(rows[0].staffState == "离职"){
					$.messager.alert("提示","你不能修改已离职员工的信息！");
				}else{
					if(rows.length ==1 ){
						
						$("#hwc-addstaffwin").window({'title':"编辑员工"});
						$("#hwc-addstaffwin").window("open");
						
						//初始化表单
						var staffNo = rows[0].staffNo;
						
						$.getJSON("findStaffById", {"staffNo": staffNo}, function(data){
							$("#form-addStaff").form("load", data);
							//$("#hwc-staff-dept").combogrid({value:data.Tbdepartment.departmentNo});
						});
					}else{
						
						$.messager.alert("提示", "您一次只能编辑一条信息，您目前选择了" + rows.length + "条信息!");
					}
				}
		}});
		//删除员工
		$("#hwc-deleteStaff").linkbutton({
			onClick:function(){
				$.messager.confirm("提示","您确定要删除吗？",
				function(v){
					if(v){
						//获取所有选中的编号
						var rows = $("#hwc-staff").datagrid("getSelections");
						//将编号保存到数组
						var arr = [];
						for(var i=0;i< rows.length;i++){
							arr.push(rows[i].staffNo);
						}
						//用form提交
						$('<form>').form('submit',{
							url:'deleteStaff',
							onSubmit:function(param){
									param.list = arr;
							},
							success:function(data){
								var json = eval("("+data+")");
								$.messager.alert("提示","删除成功！");
								$("#hwc-staff").datagrid("reload");
							}
						});
					}
				});
			}
		});
	
	//员工离职
		$("#hwc-quitStaff").linkbutton({
			onClick:function(){
				var rows = $("#hwc-staff").datagrid("getSelections");
				if(rows[0].staffState == "离职"){
					$.messager.alert("提示","你不能对已离职员工再次本操作！");
				}else{
					
				$.messager.confirm("提示","您确定要将该员工的状态改为离职吗？",
				function(v){
						if(v){
							//获取选中的编号
							
							//将编号保存到数组
							var arr =[];
							for(var i = 0; i<rows.length;i++){
								arr.push(rows[i].staffNo);
								
							}
							//用form提交
							$("<form>").form('submit',{
								
								url:'quitStaff',
								
								onSubmit:function(param){
									
									param.list = arr;
								},
								success:function(data){								
									var json = eval("("+data+")");
									$.messager.alert("提示","离职操作成功！");
									$("#hwc-staff").datagrid("reload");
								}
							});
						}});	
				}	
			
			}
				});
		//员工密码重置
		$("#hwc-resetStaff").linkbutton({
			onClick:function(){
				$.messager.confirm("提示","您确定要重置该员工的密码吗？",function(v){
					if(v){
						//获取选中编号
						var rows = $("#hwc-staff").datagrid("getSelections");
						//将编号保存到数组
						var arr = [];
						for(var i=0;i<rows.length;i++){
							arr.push(rows[i].staffNo);
						}
						//用form提交
						$("<form>").form('submit',{
							url :'resetStaffpwd',
							onSubmit:function(param){
								param.list = arr;
							},
							success:function(data){
								var json = eval("("+data+")");
								$.messager.alert("提示","密码重置成功！");
								$("#hwc-staff").datagrid("reload");
							}
						});
					}});
				}
		});
		//显示全部
		$("#hwc-allStaff").linkbutton({
			onClick:function(){
				$("#hwc-staff").datagrid({
					url:'queryStaffByPage',
					method:'get'
				});
			}
		});
	
		
	});
</script>


	<table id="hwc-staff" class="easyui-datagrid" 
		data-options = " 
		url:'queryStaffByPage',
		fitColumns:true,
		fit:true,
		pagination:true,
		pageSize:5 ,
		pageList:[5,10,15,20],
		rownumbers:true,
		toolbar:'#hwc-staffthand',
		border:false
		">
		<thead>
			<tr>
				<th data-options="field:'staffNo', checkbox:true">员工编号</th>
				<th data-options="field:'staffName',width:'10%',align:'center'">姓名</th>
				<th data-options="field:'departmentName',width:'12%',align:'center'">所属科室</th>
				<th data-options="field:'staffAccount',width:'13%',align:'center'">账号</th>
				<th data-options="field:'staffAge',width:'5%',align:'center'">年龄</th>
				<th data-options="field:'staffIdcard',width:'15%',align:'center'">身份证</th>
				<th data-options="field:'staffPhone',width:'10%',align:'center'">电话</th>
				<th data-options="field:'postName',width:'7%',align:'center'">职务</th>
				<th data-options="field:'roleName',width:'7%',align:'center'">角色</th>
				<th data-options="field:'staffSex',width:'5%',align:'center'">性别</th>
				<th data-options="field:'staffState',width:'7%',align:'center',
				styler:function(value,row,index){
					if(row.staffState == '离职'){
						return 'color:red;';
					}	
				}	
				
				">状态</th>
				<th data-options="field:'titleName',width:'8%',align:'center',required:true">职称</th>
				
			</tr>
		</thead>
	</table>
	<div id="hwc-staffthand">
	
			<a id="hwc-addStaff" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加员工</a>
			<a id="hwc-editStaff" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">编辑员工</a>
			<a id="hwc-deleteStaff" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true">批量删除</a>
			<a id="hwc-quitStaff" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true">离职复职</a>
			<a id="hwc-allStaff" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">显示全部</a>
			<a id="hwc-resetStaff" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">重置密码</a>
			<input id="hwc-likeStaff"class="easyui-searchbox" data-options="prompt:'搜索'" style="width:300px"></input>
			<div id="hwc-staffmm" style="width: 120px">
				<div data-options="name:'findNameStaff'">通过姓名查询员工</div>
				<div data-options="name:'findDeptStaff'">通过部门查询员工</div>
			</div>
			 <script>
				<!---->
				$("#hwc-likeStaff").searchbox({
					searcher:function(value,name){
						$("#hwc-staff").datagrid({
							url:name =='findDeptStaff' ?  'findDepartmentNameByPage' :'findStaffNameByPage',
							method:'post',
							queryParams:{
								staff:value
							},
							onLoadSuccess:function(){
								
								var t = $("#hwc-staff").datagrid("getRows");
								var d=t[0];
								if(d == null){
									$.messager.alert("提示","不存在该关键字的记录！");
									
									
								}
								
								}});
							$(this).searchbox('setValue','');
					},
					menu : '#hwc-staffmm',
					prompt : '搜索员工信息',	
				});
				
			</script> 
	</div>
	<!-- 添加窗口 -->
	<div	id="hwc-addstaffwin" class="easyui-window" title="添加员工" data-options="
		collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-save',
		closed: true" style="width:400px;height:290px;padding:5px;">
		
		
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'center'" style="padding:10px">
				<form id="form-addStaff" method="post">
				
			
					<div	style="display:none;">
							<input  name = "staffNo" class ="easyui-textbox"  data-options="label:'员工编号: ',labelWidth:'50',labelAlign:'center'" >
					</div>
					<div id="hwc-staffitem">
						<input style="width:130px;"name = "staffName" class ="easyui-textbox" data-options="label:'姓名: ',labelWidth:'50',labelAlign:'center',required:true">
						<input name = "staffPhone" class="easyui-textbox" data-options="label:'电话: ',labelWidth:'50',labelAlign:'center',required:true">
					</div> 
					<div id="hwc-staffitem">
						<input	id="hwc-idcard" name="staffIdcard" class="easyui-textbox" data-options="label:'身份证:',labelWidth:'50',labelAlign:'center',width:'310',required:true" />						
					</div>
					<div	id="hwc-staffitem">
					<!--性别-->
					<div>
						<select id="hwc-staffsex"  name="staffSex" class="easyui-combobox" style="width:100px;height:20px;" data-options="label: ' 性别: ',labelWidth:'50',labelAlign:'center',panelHeight:42,required:true">
							<option value="男">男</option>
							<option value="女">女</option>
						</select>
					</div>
					<div id="hwc-age-div">
					<!-- 年龄 -->	
						<input  id="hwc-age"name="staffAge" class ="easyui-textbox" data-options="label:'年龄: ',labelWidth:'50',labelAlign:'center',width:'100',required:true" style="width:150px">
					<!-- 科室 -->
					</div>
						
					</div>
					<div >
					
					<select name="departmentNo" id="hwc-staff-dept" class="easyui-combogrid"  style="width:150px;"
							data-options="
							label:'科室: ',
							labelWidth:'50',
							labelAlign:'center',
							width:'150',
							panelWidth:'300',
							panelHeight:'215',
							required:true,
							pagination:true,
							pageSize:5 ,
							pageList:[5,10,15,20],
							rownumbers:true,
							idField:'departmentNo',
							textField:'departmentName',
							url:'queryByDepartmentPage',
							columns:[[
								{field:'departmentNo',title:'科室编号',width:95,hidden:true},
								{field:'departmentName',title:'科室名称',width:130},
								{field:'address',title:'科室地址',width:135}
							]]
							"></select>
							
					<select name="staffPostNo" id="hwc-staff-post" class="easyui-combogrid"  style="width:150px;"
						data-options="
						label:'职务: ',
						labelWidth:'50',
						labelAlign:'center',
						width:'150',
						panelWidth:'110',
						panelHeight:'200',
						required:true,
						idField:'postNo',
						textField:'postName',
						url:'queryAllPost',
						columns:[[
							{field:'postNo',title:'职务编号',width:100,hidden:true},
							{field:'postName',title:'职务名称',width:100}
						]]
						"></select>
					
				
						
					<div id="hwc-staffitem">
						<select name="staffTitle" id="hwc-staff-title" class="easyui-combogrid" style="width:150px;"
							data-options="
							label:'职称:',
							labelWidth:'50',
							labelAlign:'center',
							width:'150',
							panelWidth:105,
							panelHeight:'auto',
							required:true,
							idField:'titleNo',
							textField:'titleName',
							url:'queryAllTitle',
							columns:[[
								{field:'titleNo',title:'职称编号',width:50,hidden:true},
								{field:'titleName',title:'职称名称',width:100},
								{field:'postno',title:'职务ID',width:100,hidden:true}
							]]
							"></select>
							
							
						<select name="staffRole" id="hwc-staff-role" class="easyui-combogrid"  style="width:150px;"
							data-options="
							label:'角色: ',
							labelWidth:'50',
							labelAlign:'center',
							width:'150',
							panelWidth:213,
							panelHeight:'200',
							required:true,
							idField:'roleNo',
							textField:'roleName',
							url:'queryAllRole',
							columns:[[
								{field:'roleNo',title:'角色编号',width:50,hidden:true},
								
								{field:'roleName',title:'角色名称',width:100},
								
								{field:'roleExplain',title:'角色备注',width:100}
								
							]]
							"></select>
						</div>
					</div>	
				</form>
			</div>
			
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a id="hwc-staff-okbtn" class="easyui-linkbutton" data-options="iconCls:'icon-ok', plain:true" >确定</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel', plain:true" href="javascript:$('#hwc-addstaffwin').window('close');" onclick="">取消</a>
			</div>
			
		</div>		
	</div>
	
	
</body>
</html>